import com.speljohan.rsbot.script.Random;
import com.speljohan.rsbot.script.wrappers.RSInterface;
import com.speljohan.rsbot.script.wrappers.RSInterfaceChild;
import com.speljohan.rsbot.script.wrappers.RSNPC;
import com.speljohan.rsbot.script.wrappers.RSTile;
import com.speljohan.rsbot.accessors.NPC;
import com.speljohan.rsbot.bot.Bot;

public class Mime extends Random {

	int animation;

	public RSTile MimeTile = new RSTile(2008, 4762);

	int status = 0;

	@Override
	public boolean activateCondition() {

		if (!isLoggedIn()) {
			return false;
		}

		if (distanceTo(MimeTile) <= 15) {
			return true;
		} else {
			return false;
		}

	}

	public boolean ClickAnimation(final String find) {
		for (int a = 0; a < MimeInterface().getChildCount(); a++) {
			final RSInterfaceChild child = getInterface(188, a);
			if (child.getText().contains(find)) {
				return atInterface(child);
			}
		}
		return false;
	}

	@Override
	public String getAuthor() {
		return "Pwnaz0r";
	}

	@Override
	public String getName() {
		return "Mime";
	}

	@Override
	public double getVersion() {
		return 1.12;
	}

	public void listen(final int timer) {
		if (MimeInterface().isValid()) {
			return;
		}
		final long start = System.currentTimeMillis();
		while (System.currentTimeMillis() - start >= timer) {
			if (MimeInterface().isValid()) {
				wait(random(800, 1200));
				return;
			}
		}
	}

	@Override
	public int loop() {
		RSNPC n = getNearestNPCByName("Mime");
		if(n == null)
			n = getNPCAt(new RSTile(2011, 4762));
		if (n == null || !(distanceTo(MimeTile) <= 15)) {
			log.info("Mime not found!");
			return -1;
		}

		if (getInterface(372, 2).getText().contains("Watch") || getInterface(372, 3).getText().contains("Watch")) {
			if (n.getAnimation() != -1 && n.getAnimation() != 858) {
				animation = n.getAnimation();
				log.info("New Mime Animation: " + animation);
				wait(1000);
				listen(5000);
				return random(300, 600);
			}
			return random(400, 600);
		}

		if (MimeInterface().isValid()) {
			log.info("Clicking Text According to Animation: " + animation);
			if (animation != -1) {
				switch (animation) {
				case 857:
					ClickAnimation("Think");
					break;
				case 860:
					ClickAnimation("Cry");
					break;
				case 861:
					ClickAnimation("Laugh");
					break;
				case 866:
					ClickAnimation("Dance");
					break;
				case 1128:
					ClickAnimation("Wall");
					break;
				case 1129:
					ClickAnimation("Lean");
					break;
				case 1130:
					ClickAnimation("Rope");
					break;
				case 1131:
					ClickAnimation("Box");
					break;
				default:
					log.info("Unknown Animation: " + animation);
					return random(2000, 3000);
				}
			}
		}

		return random(500, 1000);
	}
	
	public RSNPC getNPCAt(RSTile t) {
		final int[] validNPCs = Bot.getClient().getNPCIndexArray();
		final NPC[] npcs = Bot.getClient().getNPCArray();
		for (final int element : validNPCs) {
			if (npcs[element] == null) {
				continue;
			}
			RSNPC Monster = new RSNPC(npcs[element]);
			try {
				if(Monster.getLocation().equals(t))
					return Monster;
				/*for (final int id : ids) {
					if (id != Monster.getID() || Monster.isInCombat()) {
						continue;
					}
					final int distance = distanceTo(Monster);
					if (distance < Dist) {
						Dist = distance;
						closest = Monster;
					}
				}*/
			} catch (final Exception e) {
				// e.printStackTrace(); TODO: Why?
			}
		}
		return null;
	}	

	public RSInterface MimeInterface() {
		return RSInterface.getInterface(188);
	}
}